<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace api\common\controller;

use cmf\controller\RestBaseController;
use cmf\lib\Storage;
use think\Db;
use think\db\Query;
use think\facade\Cache;
use think\facade\Request;
use think\Validate;

class CommonController extends RestBaseController
{
    public static function timeFormat($timeStamp)
    {
        $now = time();
        $time = $now - $timeStamp;
        if($time < 60){
            return $time.'秒前';
        }else if($time < 60*60){
            return number_format($time/60,0).'分钟前';
        }
        else if($time < 60*60*24){
            return number_format($time/3600,0).'小时前';
        }
        else if($time < 60*60*24*30){
            return number_format($time/3600/24,0).'天前';
        }else{
            return date('Y月d日',$timeStamp);
        }
    }

    public static function uploadFile($imagePath,$file)
    {

        $storage = cmf_get_option('storage');
        //是否要上传到云存储
        if ( $storage['type'] != 'Local') { 
            $storageObj   = new Storage($storage['type'], $storage['storages'][$storage['type']]);
            
            $result = $storageObj->upload($imagePath, $file, 'image');
            if ( $storage['type'] == 'Qiniu') {
                $qiniuSetting   = cmf_get_plugin_config('Qiniu');
                if(!$qiniuSetting){
                    return ('七牛云存储插件异常');
                }
                $count = substr_count($result['preview_url'],'http');//出现http或https的次数
                if($count>1){
                    if(strpos($result['preview_url'],'https')){
                        $url = explode($qiniuSetting['style_separator'],substr($result['preview_url'],8));
                    }else{
                        $url = explode($qiniuSetting['style_separator'],substr($result['preview_url'],7));
                    }
                    $result['url'] = $result['preview_url'] = $url['0'];
                }
                $result['url'] = $result['preview_url'] = $url['0'];
                
            }
        }else{
            $result['type'] ='local';
            $result['url']= $result['preview_url'] = cmf_get_file_download_url($imagePath);
        }

        if ($result['preview_url']) {
            return $result;
        } else {
            return "上传失败!";
        }
    }
}